UIActivityPublicBeta = Class("UIActivityPublicBeta", UIComponentBase)

function UIActivityPublicBeta:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.banner = self:GetChild("bannerBg")
    self.bannerChar = self:GetChild("bannerChar")
    self.bannerText = self:GetChild("bannerText")
    self.title = self:GetChild("title")
    self.bannerTips = {};
    for i = 1, 8 do
        table.insert(self.bannerTips, self:GetChild("bannerTips"..i));
    end
    self.giftList = self:SetDynamicList(self:GetChild("list"),self.updateItem, nil, true)
    self.giftList.scrollPane.onScroll:Add(self.OnListScroll, self)
    self.tipTypeController = self:GetController("tipType")
    self.isFirst = true;
end

function UIActivityPublicBeta:OnListScroll()
    if self.endItems ~= nil then
        local isBottomMost = self.giftList.scrollPane.isBottomMost;
        if self.itemNum == 3 or isBottomMost then
            self.parent:updateListPoint(false);
            self.pointValue = false;
        else
            local flag = false;
            for k, v in pairs(self.endItems) do
                if self.giftList:IsChildInView(v) then
                    flag = true;
                    break;
                end
            end
            if flag then
                if self.pointValue == nil or self.pointValue then
                    self.pointValue = false;
                    self.parent:updateListPoint(false);
                end
            else
                if self.pointValue == nil or not self.pointValue then
                    self.pointValue = true;
                    self.parent:updateListPoint(true);
                end
            end
        end
    end
end

function UIActivityPublicBeta:Refresh()
    self.startItem = nil;
    self.endItems = nil;
    self.endItems = {};
    self.pointValue = nil;
    logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.tipTypeController.selectedIndex = 1;
    self:RemoveAllTimeCall();
    if self.modelTableData.f_BannerTipsType == 0 then
        self.tipTypeController.selectedIndex = 0;
    elseif self.modelTableData.f_BannerTipsType == 1 then
        if self.severData.nextRefreshTime == nil or self.severData.nextRefreshTime == 0 then
            self.countDownNum = self.severData.endTime - HelperFunc.GetServerTime();
        else
            self.countDownNum = self.severData.nextRefreshTime - HelperFunc.GetServerTime();
        end
        if self.countDownNum <= 0 then
            self.bannerText.text = getLanguage("Sys_ActivityEnd");
        else
            self:TimeCall(self.OnCountDown, 1000, -1);
            self.bannerText.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), HelperFunc.GetSurpTimeStr(self.countDownNum));
        end
    elseif self.modelTableData.f_BannerTipsType == 1 then
        self.countDownNum = os.date("%Y/%m/%d %H:%S-%Y/%m/%d %H:%S",self.severData.startTime, self.severData.endTime)
        self.bannerText.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), self.countDownNum);
    end
    self.bannerChar.url = tostring(self.modelTableData.f_BannerHead);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    for k, v in pairs(decodeJsonStrToTabel(self.modelTableData.f_BannerTipsShow)) do
        if self.bannerTips[k] ~= nil then
            if v ~= "" then
                self.bannerTips[k].url = tostring(v);
            else
                self.bannerTips[k].url = "";
            end
        else
            break;
        end
    end
    self.title.text = getLanguage(self.modelTableData.f_Title, nil, "");
    self.itemNum = #self.severData.items;
    self.giftList.numItems = self.itemNum;
    HelperFunc.ListTranstionPlay(self.giftList);
    if self.isFirst then
        self.isFirst = false;
        self.giftList:ScrollToView(0, false);
    end
    self:OnListScroll();
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine)
end

function UIActivityPublicBeta:updateItem(index, itemObj)
    local serverData = self.severData.items[index];
    local tableData = self.itemData[tostring(serverData.subId)];
    if tableData == nil then
        itemObj.visible = false;
        return ;
    end
    local bg = itemObj:GetChild("bg");
    local mask = itemObj:GetChild("mask");
    local buy = itemObj:GetChild("numMoney");
    local ntext = itemObj:GetChild("ntext");
    local buyNum = itemObj:GetChild("numLimit");
    local fristItem = itemObj:GetChild("item");
    local char = itemObj:GetChild("char");
    local itemList = itemObj:GetChild("itemList");
    --local discount = itemObj:GetChild("discount");
    local original = itemObj:GetChild("original");
    local originalMoney = itemObj:GetChild("originalMoney");
    local stateController = itemObj:GetController("state");
    local typeController = itemObj:GetController("type");
    local discountController = itemObj:GetController("discount");
    local isfreeController = itemObj:GetController("isfree");
    local freeCtrl = itemObj:GetController("IsFree");
    local isShowController = itemObj:GetController("isShow");
    local freeText = itemObj:GetChild("freetext");
    buyNum.text = serverData.residue.."/"..tableData.f_Count;
    if serverData.residue <= 0 then
        stateController.selectedIndex = 1;
        table.insert(self.endItems, itemObj)
    else
        stateController.selectedIndex = 0;
    end
    bg.url = tostring(tableData.f_BgPic);
    mask.url = tostring(tableData.f_BgPic);
    bg.onClick:Clear();
    isShowController.selectedIndex = tableData.f_Heroimage
    local reward;
    if tableData.f_ActivityId ~= nil and tableData.f_ActivityId ~= "" and tableData.f_NewMajorCommodityReward ~= nil and tableData.f_NewMajorCommodityReward ~= "" then
        local activityId = tableData.f_ActivityId
        local newReward = decodeJsonStrToTabel(tableData.f_NewMajorCommodityReward);
        local newRewardNum = #newReward;
        for i = 1, #activityId do
            --local actTableData, actServerData = logicMgr.ActiveManager.getActiveData(activityId[i]);
            local isEnd = logicMgr.ActiveManager.IsActiveEnd(activityId[i]);
            if not isEnd and i <= newRewardNum then
                reward = decodeJsonStrToTabel(tableData.f_NewMajorCommodityReward)[i][1];
                break;
            end
        end
    end
    if reward == nil then
        reward = decodeJsonStrToTabel(tableData.f_MajorCommodityReward)[1];
    end
    if tableData.f_Type == 1 then
        typeController.selectedIndex = 1;
        char.url = tostring(tableData.f_BannerHeroHead);
        bg.onClick:Add(function()
            showUI(PanelNames.UIActivityPublicConfirm, nil, self.activeId, serverData.subId);
        end);
        --local mainId = decodeJsonStrToTabel(tableData.f_MajorCommodityReward)[1];
        logicMgr.CostManager.RefreshItem(fristItem, {reward[1] + 201500});
    else
        typeController.selectedIndex = 0;
        if tableData.f_Type == 2 then
            typeController.selectedIndex = 2;
        end
        bg.onClick:Add(function()
            if serverData.countSum ~= nil and serverData.countSum == -10 then
                local data = {};
                table.insert(data, serverData.subId);
                logicMgr.CostManager.getRewardById(self.activeId, data, self.onBuySuccess, self);
            else
                self:ClickBuy(serverData.subId, tableData.f_ProductId, 1, tableData.f_Price);
            end
        end);
        --logicMgr.CostManager.RefreshItem(fristItem, decodeJsonStrToTabel(tableData.f_MajorCommodityReward)[1]);
        logicMgr.CostManager.RefreshItem(fristItem, reward);
    end
    if serverData.countSum == nil or serverData.countSum < 0 then
        freeCtrl.selectedIndex = 0;
    else
        freeCtrl.selectedIndex = 1;
        freeText.text = string.format(getLanguage("storedesc1020201"), serverData.countSum);
    end
    if tableData.f_OriginPrice == nil or tableData.f_OriginPrice == 0 then
        discountController.selectedIndex = 0;
    else
        discountController.selectedIndex = 1;
        originalMoney.text = tableData.f_OriginPrice;
       -- discount.text = tableData.f_Discount.."%";
    end
    ntext.text = getLanguage("SellLimit");
    if tableData.f_Price == 0 then
        isfreeController.selectedIndex = 1;
        buy.text = getLanguage("FreeMoney");
    else
        if serverData.countSum ~= nil and serverData.countSum == -10 then
            isfreeController.selectedIndex = 1;
            buy.text = getLanguage("FreeMoney");
        else
            isfreeController.selectedIndex = 0;
            buy.text = "￥"..tableData.f_Price;
        end
    end
    local listData = decodeJsonStrToTabel(tableData.f_CommonCommodityReward);
    logicMgr.CostManager.RefreshItemList(itemList, listData);

    --判断道具是否皮肤，如果是看是否拥有这个皮肤
    local isHave = false
    for k,v in pairs(listData) do
        local itemType = dataTable.getDataTableByStringId("t_item",v[1],"f_BagType")
        if itemType and tonumber(itemType) == 256 then
            local skinInfo = logicMgr.SkinManager.getHeroSkinSrvInfoBySkinId(v[1])
            if skinInfo and skinInfo.overtime and skinInfo.overtime == -1 then
                isHave = true
                break
            end
        end
    end
    if reward and reward[1] then
        local itemType = dataTable.getDataTableByStringId("t_item",reward[1],"f_BagType")
        if itemType and tonumber(itemType) == 256 then
            local skinInfo = logicMgr.SkinManager.getHeroSkinSrvInfoBySkinId(reward[1])
            if skinInfo.overtime and skinInfo.overtime == -1 then
                isHave = true
            end
        end
    end
    if isHave then
        bg.onClick:Clear()
        bg.onClick:Add(function()
            UISysTips.AddMsg(getLanguage("SkinDesc10001"))
        end)
    end
    --if #listData == 0 then
    --    itemList.width = 0;
    --else
    --    local minNum = 3;
    --    if tableData.f_Type == 2 then
    --        minNum = 2;
    --    end
    --    itemList.width = math.min(#listData, minNum) * (itemList:GetChildAt(0).width + itemList.columnGap) - itemList.columnGap;
    --end
end

function UIActivityPublicBeta:OnCountDown()
    if self.modelTableData.f_BannerTipsType ~= 1 then
        return true;
    end
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.bannerText.text = getLanguage("Sys_ActivityEnd");
        return true;
    end
    self.bannerText.text = string.format(getLanguage(self.modelTableData.f_BannerTips, nil, "%s"), HelperFunc.GetSurpTimeStr(self.countDownNum));
end

function UIActivityPublicBeta:ClickBuy(id, payId, num, money)
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, id, payId, num, money, self.onBuySuccess, self);
end

function UIActivityPublicBeta:onBuySuccess()
end

-- 卸载
function UIActivityPublicBeta:OnDestroy()
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
    self.giftList.scrollPane.onScroll:Remove(self.OnListScroll)
    self.endItems = nil;
end